package com.kelvin.kelvinlib.imageloader;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.support.v4.graphics.drawable.RoundedBitmapDrawable;
import android.support.v4.graphics.drawable.RoundedBitmapDrawableFactory;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.target.BitmapImageViewTarget;
import com.kelvin.kelvinlib.bitmap.BitmapUtil;

import java.io.File;

/**
 * Created by kelvin on 2017/1/18.
 */

public class GlideUtil {
    /**
     * 用glide加载圆形图片
     *
     * @param url
     * @param iv
     * @param context
     */
    public static void loadImgCircle(String url, final ImageView iv, final Context context, int res) {
        Glide.with(context).load(url).asBitmap().placeholder(res).centerCrop().diskCacheStrategy(DiskCacheStrategy.SOURCE)
                .into(new BitmapImageViewTarget(iv) {
                    @Override
                    protected void setResource(Bitmap resource) {
                        RoundedBitmapDrawable circularBitmapDrawable =
                                RoundedBitmapDrawableFactory.create(context.getResources(), resource);
                        circularBitmapDrawable.setCircular(true);
                        iv.setImageDrawable(circularBitmapDrawable);
                    }

                    @Override
                    public void onLoadStarted(Drawable placeholder) {
//                super.onLoadStarted(placeholder);
                        RoundedBitmapDrawable circularBitmapDrawable =
                                RoundedBitmapDrawableFactory.create(context.getResources(), BitmapUtil.drawable2Bitmap(placeholder));
                        circularBitmapDrawable.setCircular(true);
                        iv.setImageDrawable(circularBitmapDrawable);
                    }
                });
    }


    /**
     * 用glide加载圆形图片
     *
     * @param path
     * @param iv
     * @param context
     */
    public static void loadImgCircleFile(String path, final ImageView iv, final Context context, int res) {
        Glide.with(context).load(Uri.fromFile(new File(path))).asBitmap().placeholder(res).centerCrop().diskCacheStrategy(DiskCacheStrategy.SOURCE)
                .into(new BitmapImageViewTarget(iv) {
                    @Override
                    protected void setResource(Bitmap resource) {
                        RoundedBitmapDrawable circularBitmapDrawable =
                                RoundedBitmapDrawableFactory.create(context.getResources(), resource);
                        circularBitmapDrawable.setCircular(true);
                        iv.setImageDrawable(circularBitmapDrawable);
                    }

                    @Override
                    public void onLoadStarted(Drawable placeholder) {
//                super.onLoadStarted(placeholder);
                        RoundedBitmapDrawable circularBitmapDrawable =
                                RoundedBitmapDrawableFactory.create(context.getResources(), BitmapUtil.drawable2Bitmap(placeholder));
                        circularBitmapDrawable.setCircular(true);
                        iv.setImageDrawable(circularBitmapDrawable);
                    }
                });
    }

    /**
     * 用glide加载正常图片
     *
     * @param path
     * @param iv
     * @param context
     */
    public static void loadCommonImgFile(String path, final ImageView iv, final Context context, int res) {
        Glide.with(context).load(Uri.fromFile(new File(path))).asBitmap().placeholder(res).centerCrop().
                diskCacheStrategy(DiskCacheStrategy.SOURCE).into(iv);
    }


    /**
     * 正常圆角加载图片
     *
     * @param url
     * @param iv
     * @param context
     * @param res
     */

    public static void loadCommonImg(String url, int res, final ImageView iv,
                                     final Context context) {
//        Glide
//                .with(context)
//                .load(url)
//                .transform(new GlideRoundTransform(context, 10))
////                .transform(new GlideCircleTransform(mContext))
////                .centerCrop() //这个会影响transform
//                .placeholder(res)
//                .error(res)
//                .crossFade()
//                .into(iv);
        Glide.with(context).load(url).asBitmap().placeholder(res).centerCrop().diskCacheStrategy(DiskCacheStrategy.SOURCE).into(new BitmapImageViewTarget(iv) {
            @Override
            protected void setResource(Bitmap resource) {
                RoundedBitmapDrawable roundedBitmapDrawable =
                        RoundedBitmapDrawableFactory.create(context.getResources(), resource);
                roundedBitmapDrawable.setCornerRadius(10);
                iv.setImageDrawable(roundedBitmapDrawable);
            }

            @Override
            public void onLoadStarted(Drawable placeholder) {
//                super.onLoadStarted(placeholder);
                RoundedBitmapDrawable roundedBitmapDrawable =
                        RoundedBitmapDrawableFactory.create(context.getResources(), BitmapUtil.drawable2Bitmap(placeholder));
                roundedBitmapDrawable.setCornerRadius(10);
                iv.setImageDrawable(roundedBitmapDrawable);
            }
        });
    }


    /**
     * 正常圆角加载图片
     *
     * @param url
     * @param iv
     * @param context
     * @param res
     */

    public static void loadCommonImgWithOutCenterCrop(String url, int res, final ImageView iv, final Context context) {
//        Glide
//                .with(context)
//                .load(url)
//                .transform(new GlideRoundTransform(context, 10))
////                .transform(new GlideCircleTransform(mContext))
////                .centerCrop() //这个会影响transform
//                .placeholder(res)
//                .error(res)
//                .crossFade()
//                .into(iv);
        Glide.with(context).load(url).asBitmap().placeholder(res).diskCacheStrategy(DiskCacheStrategy.SOURCE).into(new BitmapImageViewTarget(iv) {
            @Override
            protected void setResource(Bitmap resource) {
                RoundedBitmapDrawable roundedBitmapDrawable =
                        RoundedBitmapDrawableFactory.create(context.getResources(), resource);
                roundedBitmapDrawable.setCornerRadius(10);
                iv.setImageDrawable(roundedBitmapDrawable);
            }

            @Override
            public void onLoadStarted(Drawable placeholder) {
//                super.onLoadStarted(placeholder);
                RoundedBitmapDrawable roundedBitmapDrawable =
                        RoundedBitmapDrawableFactory.create(context.getResources(), BitmapUtil.drawable2Bitmap(placeholder));
                roundedBitmapDrawable.setCornerRadius(10);
                iv.setImageDrawable(roundedBitmapDrawable);
            }
        });
    }


    /**
     * 正常圆角加载图片加上白色背景
     *
     * @param url
     * @param iv
     * @param context
     * @param res
     */
    public static void loadCommonImgWithWhiteBG(String url, int res, final ImageView iv, final Context context) {
//        Glide
//                .with(context)
//                .load(url)
//                .transform(new GlideRoundTransform(context, 10))
////                .transform(new GlideCircleTransform(mContext))
////                .centerCrop() //这个会影响transform
//                .placeholder(res)
//                .error(res)
//                .crossFade()
//                .into(iv);
        Glide.with(context).load(url).asBitmap().placeholder(res).centerCrop().diskCacheStrategy(DiskCacheStrategy.SOURCE).into(new BitmapImageViewTarget(iv) {
            @Override
            protected void setResource(Bitmap resource) {
                resource = BitmapUtil.drawBg4Bitmap(Color.parseColor("#FFFFFF"), resource);

                RoundedBitmapDrawable circularBitmapDrawable =
                        RoundedBitmapDrawableFactory.create(context.getResources(), resource);
                circularBitmapDrawable.setCircular(true);
                iv.setImageDrawable(circularBitmapDrawable);
            }

            @Override
            public void onLoadStarted(Drawable placeholder) {
//                super.onLoadStarted(placeholder);

                RoundedBitmapDrawable circularBitmapDrawable =
                        RoundedBitmapDrawableFactory.create(context.getResources(), BitmapUtil.drawable2Bitmap(placeholder));
                circularBitmapDrawable.setCircular(true);
                iv.setImageDrawable(circularBitmapDrawable);
            }
        });
    }


    /**
     * 正常没圆角加载图片
     *
     * @param url
     * @param iv
     * @param context
     * @param res
     */
    public static void loadImgWithoutRoundCorner(String url, int res, final ImageView iv, final Context context) {
//        Glide
//                .with(context)
//                .load(url)
//                .transform(new GlideRoundTransform(context, 10))
////                .transform(new GlideCircleTransform(mContext))
////                .centerCrop() //这个会影响transform
//                .placeholder(res)
//                .error(res)
//                .crossFade()
//                .into(iv);
        Glide.with(context).load(url).asBitmap().placeholder(res).centerCrop().diskCacheStrategy(DiskCacheStrategy.SOURCE).into(new BitmapImageViewTarget(iv) {
            @Override
            protected void setResource(Bitmap resource) {
                iv.setImageBitmap(resource);
            }

            @Override
            public void onLoadStarted(Drawable placeholder) {
                iv.setImageDrawable(placeholder);
            }
        });
    }

    /**
     * 正常加载背景图片
     *
     * @param url
     * @param iv
     * @param context
     * @param res
     */
    public static void loadCommonBG(String url, int res, ImageView iv, Context context) {
        Glide
                .with(context)
                .load(url)
//                .transform(new GlideRoundTransform(context, 10))
//                .transform(new GlideCircleTransform(mContext))
                .centerCrop() //这个会影响transform
                .placeholder(res)
                .error(res)
                .crossFade()
                .diskCacheStrategy(DiskCacheStrategy.SOURCE)
                .into(iv);
    }

    /**
     * 正常加载banner图
     *
     * @param url
     * @param iv
     * @param context
     * @param res
     */
    public static void loadCommonBanner(String url, int res, ImageView iv, Context context) {
        Glide
                .with(context)
                .load(url)
//                .transform(new GlideRoundTransform(context, 10))
//                .transform(new GlideCircleTransform(mContext))
                .placeholder(res)
                .error(res)
                .crossFade()
                .diskCacheStrategy(DiskCacheStrategy.SOURCE)
                .into(iv);
    }

    /**
     * 正常圆角加载图片
     *
     * @param iv
     * @param context
     * @param res
     */
    public static void loadCommonImgId(int resId, int res, final ImageView iv, final Context context) {
//        Glide
//                .with(context)
//                .load(url)
//                .transform(new GlideRoundTransform(context, 10))
////                .transform(new GlideCircleTransform(mContext))
////                .centerCrop() //这个会影响transform
//                .placeholder(res)
//                .error(res)
//                .crossFade()
//                .into(iv);
        Glide.with(context).load(resId).asBitmap().placeholder(res).centerCrop().diskCacheStrategy(DiskCacheStrategy.SOURCE).into(new BitmapImageViewTarget(iv) {
            @Override
            protected void setResource(Bitmap resource) {
                RoundedBitmapDrawable roundedBitmapDrawable =
                        RoundedBitmapDrawableFactory.create(context.getResources(), resource);
                roundedBitmapDrawable.setCornerRadius(10);
                iv.setImageDrawable(roundedBitmapDrawable);
            }

            @Override
            public void onLoadStarted(Drawable placeholder) {
//                super.onLoadStarted(placeholder);
                RoundedBitmapDrawable roundedBitmapDrawable =
                        RoundedBitmapDrawableFactory.create(context.getResources(), BitmapUtil.drawable2Bitmap(placeholder));
                roundedBitmapDrawable.setCornerRadius(10);
                iv.setImageDrawable(roundedBitmapDrawable);
            }
        });
    }

}
